<template>
  <el-container>
    <el-header>
      <div>
        <img @click="goHome()" src="../assets/logo.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="primary" round plain @click="logOut">退出</el-button>
    </el-header>
    <el-container>
      <!-- c侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleContainer">|||</div>
        <!-- 菜单 -->
        <el-menu
          :default-active="activePath"
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuList"
            :key="item.id"
          >
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState('/' + subItem.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-container>
        <el-main>
          <router-view></router-view>
        </el-main>
        <el-footer>Footer</el-footer>
      </el-container>
    </el-container>
  </el-container>
</template>

// -----------------------------------------

<script>
export default {
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  data() {
    return {
      activePath: '', // 未激活的动态地址
      isCollapse: false,
      menuList: [],
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      }
    }
  },
  methods: {
    logOut() {
      window.sessionStorage.clear()
      this.$message.success('退出成功')
      this.$router.push('/login')
    },
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) {
        return this.$message.console.error(res.meta.msg)
      }
      this.menuList = res.data
      console.log(res)
    },
    toggleContainer() {
      this.isCollapse = !this.isCollapse
    },
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    },
    goHome() {
      this.$router.push('/home')
      this.saveNavState('')
    }
  }
}
</script>

// -----------------------------------------

<style lang="less" scoped>
.page_container {
  height: 100%;
}
.el-container {
  background-color: #1e67bd;
  height: 100%;
}
.el-header {
  display: flex;
  justify-content: space-between;
  padding: 0px 16px;
  align-items: center;
  background-color: #4a90e2;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
    img {
      height: 50px;
      width: 50px;
    }
  }
}
.el-main {
  background-color: #80b1eb;
  max-height: calc(100vh - 120px);
}
.el-aside {
  background-color: #64a0e6;
}
.el-footer {
  background-color: #4a90e2;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background-color: #1f67ba;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.3em;
  cursor: pointer;
}
.exit_btn {
  background-color: cornflowerblue;
}
</style>
